/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.mysql;

import dao.TransaksiDAO;
import dao.TransaksiDetailDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import model.Transaksi;
import model.TransaksiDetail;

/**
 *
 * @author Student
 */
public class TransaksiDAOImpl implements TransaksiDAO {

    private Connection conn;
    private SimpleDateFormat formater;

    public TransaksiDAOImpl(Connection conn) {
        this.conn = conn;
        this.formater = new SimpleDateFormat("yyyy-MM-dd");
    }

    private String generateId() {
        return System.currentTimeMillis() + "";
    }

    @Override
    public int save(Transaksi transaksi) throws Exception {
        String sql = "INSERT INTO transaksi(id,tgl,customer_id) VALUES(?,?,?)";
        int out = 0;
        PreparedStatement ps = null;
        transaksi.setId(generateId());
        try {
            this.conn.setAutoCommit(false);
            ps = this.conn.prepareStatement(sql);
            ps.setString(1, transaksi.getId());
            ps.setString(2, formater.format(transaksi.getTgl()));
            ps.setInt(3, transaksi.getCustomer().getId());
            out = ps.executeUpdate();
            TransaksiDetailDAO tDao = new TransaksiDetailDAOImpl(this.conn);
            for (TransaksiDetail detail : transaksi.getDetails()) {
                tDao.save(transaksi.getId(), detail);
            }
            this.conn.commit();
        } catch (Exception ex) {
            this.conn.rollback();
            throw ex;
        } finally {
            this.conn.setAutoCommit(true);
            ps.close();
        }
        return out;

    }

    @Override
    public Transaksi getById(String id) throws Exception {
        String sql = "select id,tgl,customer_id from transaksi where id=?";
        Transaksi trans = null;
        PreparedStatement ps = null;
        try {
            ps = this.conn.prepareStatement(sql);
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                trans = new Transaksi();
                trans.setId(rs.getString(1));
                trans.setTgl(new Date(rs.getDate(2).getTime()));
                trans.setCustomer(new CustomerDAOImpl(this.conn).getById(rs.getInt(3)));
                trans.setDetails(new TransaksiDetailDAOImpl(this.conn).getByTransaksiId(rs.getString(1)));
            }
        } catch (Exception ex) {
            throw ex;
        } finally{
            ps.close();
        }
        return trans;
    }

    @Override
    public List<Transaksi> getAll() throws Exception {
        String sql = "select id,tgl,customer_id from transaksi";
        List<Transaksi> transList = new ArrayList<Transaksi>();
        PreparedStatement ps = null;
        try {
            ps = this.conn.prepareStatement(sql);           
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Transaksi trans = new Transaksi();               
                trans.setId(rs.getString(1));
                trans.setTgl(new Date(rs.getDate(2).getTime()));
                trans.setCustomer(new CustomerDAOImpl(this.conn).getById(rs.getInt(3)));
                trans.setDetails(new TransaksiDetailDAOImpl(this.conn).getByTransaksiId(rs.getString(1)));
                transList.add(trans);
            }
        } catch (Exception ex) {
            throw ex;
        } finally{
            ps.close();
        }
        return transList;
    }

    @Override
    public List<Transaksi> getByDate(Date start, Date end) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
